class Solution {
    int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

    public int romanToInt(String s) {
        int num = 0;
        for (int i = 0; i < values.length && s.length() > 0; i++) {
            while (s.startsWith(symbols[i])) {
                num += values[i];
                s = s.substring(symbols[i].length());
            }
        }
        return num;
    }
}